package com.piao.springboot_interceptor.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class TestController {

    Map<String, Object> result;

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    //@RequestParam注解标注的属性一旦没提交便会报错
    //@PostMapping("/login")等价于@RequestMapping(value = "/login",method = RequestMethod.POST)
    @PostMapping("/login")
    @ResponseBody
    public Map<String, Object> login(@RequestParam("username") String username,
                                     @RequestParam("password") String password,
                                     HttpSession httpSession) {
        result = new HashMap<>();
        System.out.println("账号" + username + " 密码" + password);

        if (username.equals("123") && "123".equals(password)) {
            System.out.println("登录成功");
            System.out.println("getResultCode " + 200);
            httpSession.setAttribute("loginUser",username);//将登陆成功的用户名添加至Session中
            result.put("resultCode", 200);
            return result;//登陆成功，前端根据resultCode跳转主页面
        } else {
            System.out.println("登录失败");
            System.out.println("getResultCode " + 500);
            result.put("resultCode", 500);
            return result;
        }
    }

    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @GetMapping("/error")
    public String error() {
        return "error";
    }

    @GetMapping("/welcome")
    public String welcome() {
        return "welcome";
    }
}
